package com.longxi.tianjintianbao.ui.fragment.houseinspection;

import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;

import com.longxi.tianjintianbao.AppConst;
import com.longxi.tianjintianbao.MyApplication;
import com.longxi.tianjintianbao.R;
import com.longxi.tianjintianbao.api.ApiRetrofit;
import com.longxi.tianjintianbao.api.response.HttpResult;
import com.longxi.tianjintianbao.exception.ExceptionHandle;
import com.longxi.tianjintianbao.exception.MySubscriber;
import com.longxi.tianjintianbao.listener.AdapterListener;
import com.longxi.tianjintianbao.model.houseinspection.YanFang;
import com.longxi.tianjintianbao.ui.activity.houseinspection.CheckRoomActivity;
import com.longxi.tianjintianbao.ui.activity.houseinspection.HomeInspectionActivity;
import com.longxi.tianjintianbao.ui.adapter.multitype.houseinspection.YanFangBinder;
import com.longxi.tianjintianbao.ui.base.BaseActivity;
import com.longxi.tianjintianbao.ui.base.BasePresenter;
import com.longxi.tianjintianbao.ui.base.LazyFragment;
import com.longxi.tianjintianbao.utils.MyUtils;
import com.longxi.tianjintianbao.utils.NListRefresh;
import com.longxi.tianjintianbao.widget.AssetStatusDialog;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadMoreListener;

import org.json.JSONException;
import org.json.JSONObject;

import me.drakeet.multitype.Items;
import me.drakeet.multitype.MultiTypeAdapter;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * Created by cxw on & 2018/9/19
 */
public class CommonFragment extends LazyFragment {

    private String type;//1入住验房 2推住验房 3 资产赔偿的
    private String state;
    private int page = 0;
    private MultiTypeAdapter adapter;
    private Items items = new Items();
    private BaseActivity mContext;
    private LazyFragment mFragment;

    @Override
    protected BasePresenter createPresenter() {
        return null;
    }

    @Override
    protected int provideContentViewId() {
        return R.layout.fragment_yanfang_common;
    }

    @Override
    protected void init(View view) {
        mContext = (HomeInspectionActivity) getActivity();
        mFragment = this;
        state = getArguments().getString("state");
        type = getArguments().getString("type");
        initAdapter();
    }

    @Override
    public void initListener() {
        super.initListener();
        mRefreshLayout.setOnRefreshLoadMoreListener(new OnRefreshLoadMoreListener() {

            @Override
            public void onRefresh(RefreshLayout refreshLayout) {
                getCheckInList(page = 0);
            }

            @Override
            public void onLoadMore(RefreshLayout refreshLayout) {
                page++;
                getCheckInList(page);
            }
        });
        getCheckInList(page = 0);
    }

    public void getCheckInList(int page){
        JSONObject object = new JSONObject();
        try {
            object.put("userid", MyApplication.loginUser.userid);
            object.put("checkintype", type);
            object.put("billtype", state);
            object.put("page", page);
            object.put("rowcount", 10);
        } catch (JSONException e) {
            e.printStackTrace();
        }

        ApiRetrofit.getInstance().getCheckInList(object)
        .subscribeOn(Schedulers.io())
        .observeOn(AndroidSchedulers.mainThread())
        .subscribe(new MySubscriber<HttpResult<YanFang>>(mContext) {
            @Override
            public void onError(ExceptionHandle.ResponeThrowable throwable) {
                MyUtils.showToast(throwable.message);
            }

            @Override
            public void onNext(HttpResult<YanFang> result) {
                items = NListRefresh.getInstance().fjData(result,
                        mRefreshLayout,
                        items,
                        adapter,
                        mFragment);

            }
        });
    }

    public void initAdapter(){
        if(adapter == null){
            adapter = new MultiTypeAdapter();
            /* 注册类型和 View 的对应关系 */
            adapter.register(YanFang.class, new YanFangBinder(listener, type));
            mRvContent.setAdapter(adapter);
        }
    }

    private AdapterListener listener = new AdapterListener() {

        @Override
        public void onClick(int position) {
            String title = "入住验房";
            switch (type) {
                case AppConst.YAN_FANG_RUZHU://入住验房
                    title = "入住验房";
                    break;
                case AppConst.YAN_FANG_TUIZHU:
                    title = "退住验房";
                    break;
                case AppConst.YAN_FANG_ZICHAN:
                    title = "资产赔偿";
                    break;
            }
            AssetStatusDialog.Builder builder = new AssetStatusDialog.Builder(mContext);
            String finalTitle = title;
            builder.setTitle(title)
            .setMessage("是否现在开始" + title + "？")
            .setNegativeButton("取消",
                    new DialogInterface.OnClickListener() {
                        @Override public void onClick(DialogInterface dialog, int which) {
                            dialog.dismiss();
                        }
                    })
            .setPositionButton("开始", new DialogInterface.OnClickListener() {
                @Override public void onClick(DialogInterface dialog, int which) {
                    dialog.dismiss();
                    YanFang yanFang = (YanFang) items.get(position);
                    Intent intent = new Intent(mContext, CheckRoomActivity.class);
                    intent.putExtra(AppConst.TITLE, finalTitle);
                    intent.putExtra(AppConst.TYPE, type);
                    intent.putExtra(AppConst.BILLID, yanFang.getBillid());
                    intent.putExtra(AppConst.STATE, state);
                    startActivityForResult(intent, AppConst.IntentRequstCode.ACTIVITY_CHECK_ROOM);
                }
            })
            .create()
            .show();
        }

        @Override
        public void onDelete(int position) {

        }
    };

    public CommonFragment setArguments(String state, String type) {
        Bundle bundle = new Bundle();
        bundle.putString("state", state);
        bundle.putString("type", type);
        setArguments(bundle);
        return this;
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if(resultCode != mContext.RESULT_OK){
            return;
        }

        switch (requestCode) {
            case AppConst.IntentRequstCode.ACTIVITY_CHECK_ROOM:
                mRefreshLayout.autoRefresh();
                break;
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        this.adapter = null;
        this.items = null;
        this.mContext = null;
        this.mFragment = null;
    }
}
